#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "ojmodel.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;
    const std::string template_path = "./ctemplate/";
    class View
    {
        public:
        //产出一个html
        bool AllToHtml(const std::vector<Question>& qs,std::string* html)
        {
            // 题目的编号 题目的标题 题目的难度
            // 推荐使用表格显示
            // 1. 形成路径
            std::string src_html = template_path + "all_questions.html";
            // 2. 形成数字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto& q : qs)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            //3. 对于ctemplate，获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            //4. 对于ctemplate，要把src_html渲染到哪里
            tpl->Expand(html, &root);
            return true;
        }
        bool OneToHtml(const Question& q,std::string* html)
        {
            //1.获取路径
            std::string src_html = template_path + "one_question.html";
            //2.形成字典
            ctemplate::TemplateDictionary root("one_question");

            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            //3.对于ctemplate，要知道src_html在哪里
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);
            //4.对于ctemplate，要把src_html渲染
            tpl->Expand(html,&root);
            return true;
        }
    };
}
